package com.mdd.admin.config.quartz.exceution;

import com.mdd.admin.config.quartz.InvokeUtils;
import com.mdd.common.entity.crontab.Crontab;
import com.mdd.common.mapper.crontab.CrontabMapper;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;

import javax.annotation.Resource;

/**
 * 禁止并发任务
 */
@DisallowConcurrentExecution
public class QuartzDisExecution extends AbstractQuartzJob {

    @Resource
    private CrontabMapper crontabMapper;

    @Override
    protected void doExecute(JobExecutionContext context, Crontab crontab) throws Exception {
        Long startTime = System.currentTimeMillis();
        InvokeUtils.invokeMethod(crontab);
        Long endTime = System.currentTimeMillis();
        if (endTime - startTime > crontab.getTaskMaxTime()){
            crontab.setTaskMaxTime(endTime - startTime);
            crontabMapper.updateById(crontab);
        }
    }

}